package com.guojian.jdbc.sql;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnProperty(value = "pagehelper.helperDialect", havingValue = "oracle")
public class OracleParser implements SqlParser {

	/**
	 *
	 * @param sql
	 */
	@Override
	public String convertToPageSql(String sql, Integer offset, Integer limit) {
		Integer endRowNoIndex = offset + limit;
		StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append("SELECT * FROM ( ");
		sqlBuilder.append(" SELECT ROWNUM ROW_NO, TMP_PAGE.* FROM ( ");
		sqlBuilder.append(sql);
		sqlBuilder.append(" ) TMP_PAGE WHERE ROWNUM <=  " + endRowNoIndex);
		sqlBuilder.append(" ) WHERE ROW_NO >  " + offset);
		return sqlBuilder.toString();
	}

	public static void main(String[] args) {
		String sql = "select * from SYS_USER  ORDER BY user_id";
		SqlServerParser spa = new SqlServerParser();
		String newSql = spa.convertToPageSql(sql, 1, 5);
		System.out.println("newSql = " + newSql);

		OracleParser opa = new OracleParser();
		String newSql1 = opa.convertToPageSql(sql, 1, 5);
		System.out.println("newSql1 = " + newSql1);

	}
}
